package cn.edu.zzu.demo03ThreadSecurity;

import java.util.ArrayList;

/**
 * 多线程操作集合时候，出现覆盖现象
 */
public class TestUnSecurity03 {

    public static void main(String[] args) {
        ArrayList<String> threads = new ArrayList<>();

        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                //改进：将共享的数据，可变的对象加锁
                synchronized (threads) { //对象 curd的对象
                    threads.add(Thread.currentThread().getName());
                }
            }).start();
        }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(threads.size());
    }
}
